//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtil {
    public DateUtil() {
    }

    public static String formatDateTime(Date date, String format) {
        DataUtil.dex();
        if(date == null) {
            return "";
        } else if(format == null) {
            return date.toString();
        } else {
            DateFormat df = new SimpleDateFormat(format);
            return df.format(date);
        }
    }

    public static String formatY0M0D(Date date) {
        return date == null?"":formatDateTime(date, "yyyyMMdd");
    }

    public static String formatYMD(Date date) {
        return date == null?"":formatDateTime(date, "yyyy-MM-dd");
    }

    public static String formatYMDHM(Date date) {
        return date == null?"":formatDateTime(date, "yyyy-MM-dd HH:mm");
    }

    public static String formatDateTimeByDate(Date date) {
        return date == null?"":formatDateTime(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatTimeStampByDate(Date date) {
        return date == null?"":formatDateTime(date, "yyyy-MM-dd HH:mm:ss.SSS");
    }

    public static Date addMinutes(Date srcDate, int minutes) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(srcDate);
        cal.add(12, minutes);
        return cal.getTime();
    }

    public static Date getNextDayFromCurrent(Date currDate, int i) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(currDate);
        gc.add(5, i);
        return gc.getTime();
    }

    public static Date getNextMonthFromCurrent(Date current, int i) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(current);
        gc.add(2, i);
        return gc.getTime();
    }

    public static Date getNextWeekFromCurrent(Date current, int i) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(current);
        gc.add(3, i);
        return gc.getTime();
    }

    public static Date getNextYearFromCurrent(Date current, int i) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(current);
        gc.add(1, i);
        return gc.getTime();
    }

    public static Date getNextHourFromCurrent(Date current, int i) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(current);
        gc.add(10, i);
        return gc.getTime();
    }

    public static int getCurrDay() {
        Calendar now = Calendar.getInstance();
        return now.get(7);
    }

    public static Date StrToDate(String str, String format) {
        if(str != null && !"".equals(str)) {
            try {
                if(format == null || "".equals(format)) {
                    format = "yyyy-MM-dd";
                }

                SimpleDateFormat sdf = new SimpleDateFormat(format);
                return sdf.parse(str);
            } catch (ParseException var3) {
                var3.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static Date StrToDate(String str) {
        return StrToDate(str, "yyyy-MM-dd");
    }

    public static long getNumOfDays(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            long diff = Math.abs(d2.getTime() - d1.getTime());
            return diff / 86400000L;
        } catch (ParseException var7) {
            var7.printStackTrace();
            return 0L;
        }
    }

    public static boolean compare(Date d1, Date d2) {
        return d2.getTime() - d1.getTime() > 600000L;
    }

    public static String getTheDay(Date d) {
        return String.valueOf("日一二三四五六".charAt(d.getDay()));
    }

    public static String[] weekDate(int year, int week) {
        if(week >= 1 && week <= 52) {
            String[] s = new String[2];
            Calendar c = Calendar.getInstance();
            c.set(1, year);
            c.set(3, week);
            c.set(7, 1);
            s[0] = formatYMD(c.getTime());
            c.add(5, 6);
            s[1] = formatYMD(c.getTime());
            return s;
        } else {
            return null;
        }
    }

    public static String[] monthDate(int year, int month) {
        if(month >= 1 && month <= 12) {
            String[] s = new String[2];
            Calendar c = Calendar.getInstance();
            c.set(1, year);
            c.set(2, month - 1);
            c.set(5, 1);
            s[0] = formatYMD(c.getTime());
            c.add(2, 1);
            c.add(5, -1);
            s[1] = formatYMD(c.getTime());
            return s;
        } else {
            return null;
        }
    }

    public static String[] seasonDate(int year, int season) {
        if(season >= 1 && season <= 4) {
            String y = String.valueOf(year);
            String[] s = new String[2];
            switch(season) {
                case 1:
                    s[0] = y + "-01-01";
                    s[1] = y + "-03-31";
                    break;
                case 2:
                    s[0] = y + "-04-01";
                    s[1] = y + "-06-30";
                    break;
                case 3:
                    s[0] = y + "-07-01";
                    s[1] = y + "-09-30";
                    break;
                case 4:
                    s[0] = y + "-10-01";
                    s[1] = y + "-12-31";
            }

            return s;
        } else {
            return null;
        }
    }

    public static Date getFirstDayOfWeek(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(someDay);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(7, 2);
        return calendar.getTime();
    }

    public static Date getLastDatetimeOfWeek(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(someDay);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        calendar.set(7, 1);
        return calendar.getTime();
    }

    public static Date getFirstDayOfMonth(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(someDay);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(5, 1);
        return calendar.getTime();
    }

    public static Date getLastDayOfMonth(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(someDay);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(5, 1);
        calendar.add(2, 1);
        calendar.add(5, -1);
        return calendar.getTime();
    }

    public static Date getBeginOfDay(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(someDay);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date[] getDaysInInterval(Date beginDate, Date endDate) {
        List<Date> days = new ArrayList();
        int dateLength = (int)((endDate.getTime() - beginDate.getTime()) / 86400000L);
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(beginDate);

        for(int i = 0; i <= dateLength; ++i) {
            calendar.add(5, i != 0?1:0);
            days.add(calendar.getTime());
        }

        return (Date[])days.toArray(new Date[days.size()]);
    }

    public static Date[] getDaysInWeek(Date someDay) {
        List<Date> daysInWeek = new ArrayList();
        Date firstDay = getFirstDayOfWeek(someDay);
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(firstDay);

        for(int i = 0; i < 7; ++i) {
            calendar.add(7, i != 0?1:0);
            daysInWeek.add(calendar.getTime());
        }

        return (Date[])daysInWeek.toArray(new Date[daysInWeek.size()]);
    }

    public static Date[] getSevernDaysPrior(Date someDay) {
        List<Date> daysInWeek = new ArrayList();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(someDay);
        calendar.add(5, -7);
        calendar.setTime(calendar.getTime());

        for(int i = 0; i < 7; ++i) {
            calendar.add(5, 1);
            daysInWeek.add(calendar.getTime());
        }

        return (Date[])daysInWeek.toArray(new Date[daysInWeek.size()]);
    }

    public static Date[] getDaysInMonth(Date someDay) {
        List<Date> daysInMonth = new ArrayList();
        Date firstDay = getFirstDayOfMonth(someDay);
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(firstDay);

        for(int i = 0; i < getDaysCountOfMonth(someDay); ++i) {
            calendar.add(5, i != 0?1:0);
            daysInMonth.add(calendar.getTime());
        }

        return (Date[])daysInMonth.toArray(new Date[daysInMonth.size()]);
    }

    public static Date[] splitDayByHour(Date someDay) {
        List<Date> dates = new ArrayList();
        Date begin = getBeginOfDay(someDay);
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(begin);

        for(int i = 0; i < 24; ++i) {
            calendar.add(11, i != 0?1:0);
            dates.add(calendar.getTime());
        }

        return (Date[])dates.toArray(new Date[dates.size()]);
    }

    public static Date getFirstDayOfYear(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(someDay);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(6, 1);
        return calendar.getTime();
    }

    public static Date[] splitYearByMonth(Date someDay) {
        List<Date> dates = new ArrayList();
        Date fristDay = getFirstDayOfYear(someDay);
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(fristDay);

        for(int i = 0; i < 12; ++i) {
            calendar.add(2, i != 0?1:0);
            dates.add(calendar.getTime());
        }

        return (Date[])dates.toArray(new Date[dates.size()]);
    }

    public static int getDaysCountOfMonth(Date someDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(someDay);
        calendar.set(5, 1);
        calendar.roll(5, -1);
        return calendar.get(5);
    }

    public static void main(String[] args) {
        System.out.println(getFirstDayOfWeek(new Date()));
        System.out.println(getLastDatetimeOfWeek(new Date()));
        System.out.println(getFirstDayOfMonth(new Date()));
        System.out.println(getDaysCountOfMonth(new Date()));
    }
}
